
/*
 * CambiarPassword.java
 *
 * Created on 01/02/2013, 02:15:03 PM
 */
package elaprendiz.ventanas.paneles;

import elaprendiz.controllers.CUsuario;
import elaprendiz.gui.label.LabelRect;
import elaprendiz.modelbd.Usuario;
import java.awt.Component;
import javax.swing.JOptionPane;

/**
 *
 * @author Ferz elaprendiz - www.elaprendiz.net63.net
 */
public class CambiarPassword extends javax.swing.JPanel {

    /** Creates new form CambiarPassword */
    private CUsuario cus;
    private Usuario us;
    private int seActualizo = -1;
    private JOptionPane op;
    public CambiarPassword(Usuario us) {
        initComponents();
        this.us = us;
        this.cus = new CUsuario();
        lbAviso.setVisible(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        lbAviso = new elaprendiz.gui.label.LabelRect();
        panelImage1 = new elaprendiz.gui.panel.PanelImage();
        panelCurves1 = new elaprendiz.gui.panel.PanelCurves();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        passActual = new elaprendiz.gui.passwordField.PasswordField();
        passNueva = new elaprendiz.gui.passwordField.PasswordField();
        passRepetir = new elaprendiz.gui.passwordField.PasswordField();
        bntGuardar = new elaprendiz.gui.button.ButtonRect();
        bntCancelar = new elaprendiz.gui.button.ButtonRect();

        lbAviso.setBackground(new java.awt.Color(153, 0, 0));
        lbAviso.setText("labelRect1");
        lbAviso.setPreferredSize(new java.awt.Dimension(250, 17));

        setLayout(new java.awt.BorderLayout());

        panelImage1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/elaprendiz/resources/fondoazulceleste.jpg"))); // NOI18N
        panelImage1.setLayout(new java.awt.BorderLayout());

        panelCurves1.setLayout(new java.awt.GridBagLayout());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel1.setText("Contraseña Actual:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 2, 0);
        panelCurves1.add(jLabel1, gridBagConstraints);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel2.setText("Contraseña Nueva:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 2, 0);
        panelCurves1.add(jLabel2, gridBagConstraints);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel3.setText("Repita La Contraseña:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 3, 2, 0);
        panelCurves1.add(jLabel3, gridBagConstraints);

        passActual.setPreferredSize(new java.awt.Dimension(250, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 2, 3);
        panelCurves1.add(passActual, gridBagConstraints);

        passNueva.setPreferredSize(new java.awt.Dimension(250, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 2, 3);
        panelCurves1.add(passNueva, gridBagConstraints);

        passRepetir.setPreferredSize(new java.awt.Dimension(250, 21));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.gridwidth = 5;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 2, 3);
        panelCurves1.add(passRepetir, gridBagConstraints);

        bntGuardar.setBackground(new java.awt.Color(51, 153, 255));
        bntGuardar.setText("Guardar");
        bntGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntGuardarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 2, 0);
        panelCurves1.add(bntGuardar, gridBagConstraints);

        bntCancelar.setBackground(new java.awt.Color(51, 153, 255));
        bntCancelar.setText("Cancelar");
        bntCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bntCancelarActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 3;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_END;
        gridBagConstraints.insets = new java.awt.Insets(5, 0, 2, 3);
        panelCurves1.add(bntCancelar, gridBagConstraints);

        panelImage1.add(panelCurves1, java.awt.BorderLayout.CENTER);

        add(panelImage1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

private void bntGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntGuardarActionPerformed
 String pas = String.copyValueOf(this.passActual.getPassword());
 String pasN = String.copyValueOf(this.passNueva.getPassword());
 String pasR = String.copyValueOf(this.passRepetir.getPassword());
    if(!pas.isEmpty() && !pasN.isEmpty() && !pasR.isEmpty())
    {       
        if(pas.equals(us.desencriptarPass()))
        {
            if(pasN.equals(pasR))
            {
                us.setClave(pasN);
                us.encriptarPass();
                int ap = cus.actualizarPass(us);
                if(ap != 0)
                {
                  seActualizo = 1;  
                  getOptionPane();
                  op.setValue(1);
                }
            }else{
                lbAviso.setVisible(true);
                lbAviso.setText("Las contraseñas no coinciden.");
            }
        }else{
            lbAviso.setVisible(true);
            lbAviso.setText("Contraseña Incorrecta");
            passActual.requestFocus();
        }
    }else{
        lbAviso.setVisible(true);
        lbAviso.setText("No deje espacios en blanco");
        passActual.requestFocus();
    }
}//GEN-LAST:event_bntGuardarActionPerformed

private void bntCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bntCancelarActionPerformed
  getOptionPane();
  op.setValue(1);
}//GEN-LAST:event_bntCancelarActionPerformed

    public LabelRect getLbAviso() {
        return lbAviso;
    }

    public int getSeActualizo() {
        return seActualizo;
    }
    
    private void getOptionPane()
    {
        if(op != null)
        {
            return;
        }
        Component pdr =this.getParent(); 
        while(pdr.getParent() != null)
        {
            if(pdr instanceof JOptionPane)
            {
                op = (JOptionPane)pdr;
                break;
            }            
            pdr = pdr.getParent();
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private elaprendiz.gui.button.ButtonRect bntCancelar;
    private elaprendiz.gui.button.ButtonRect bntGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private elaprendiz.gui.label.LabelRect lbAviso;
    private elaprendiz.gui.panel.PanelCurves panelCurves1;
    private elaprendiz.gui.panel.PanelImage panelImage1;
    private elaprendiz.gui.passwordField.PasswordField passActual;
    private elaprendiz.gui.passwordField.PasswordField passNueva;
    private elaprendiz.gui.passwordField.PasswordField passRepetir;
    // End of variables declaration//GEN-END:variables
}
